package com.fanco.learn_datastruct_leetcode.designpattern.singleton.type5;

/**
 * @author qiaowang 2022/2/9
 * 描述:懒汉式（线程安全，同步代码块）
 * 缺点：并不能起到线程同步的作用，多线程同时进入 if判断时，会产生多个实例。
 * 实际开发中不建议使用
 */
public class Singleton05 {
    public static void main(String[] args) {
        Singleton singleton = Singleton.getInstance();
        Singleton singleton1 = Singleton.getInstance();
        System.out.println(singleton1 == singleton);
        System.out.println(singleton1.hashCode());
        System.out.println(singleton.hashCode());
    }
}

class Singleton {
    private static Singleton instance;

    private Singleton () {

    }

    public static Singleton getInstance() {
        if (instance == null) {
            synchronized (Singleton.class) {
                instance = new Singleton();
            }
        }
        return instance;
    }
}
